package com.itheima.controller;

/*
 * 角色管理
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleManagementService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/rolemanagement")
public class RoleManagementController {
    @Reference
    private RoleManagementService roleManagementService;
    //新增角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleManagementService.add(role,permissionIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_ROLEMANAGEMENT_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLEMANAGEMENT_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleManagementService.pageQuery(queryPageBean);
    }


    //根据id查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role=roleManagementService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLEMANAGEMENT_SUCCESS,role);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_ROLEMANAGEMENT_FAIL);
        }

    }

    //根据角色id查询角色包含的多个权限ID
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer>permissionIds =roleManagementService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permissionIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }

    }

    //编辑角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleManagementService.edit(role,permissionIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_ROLEMANAGEMENT_FAIL);//编辑失败
        }
        return new Result(true, MessageConstant.EDIT_ROLEMANAGEMENT_SUCCESS);//编辑成功

    }

    //删除角色
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleManagementService.delete(id);
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_ROLEMANAGEMENT_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLEMANAGEMENT_SUCCESS);
    }
    //查询所有的角色数据
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Role> list;
        try {
            list = roleManagementService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有角色失败！", null);
        }
        return new Result(true,"查询所有角色成功！",list);
    }

}
